#ifndef WORKTABLE
#define WORKTABLE

/*!
 * \file WorkTable.h
 * \author aimery aviles / thomas raynal
 * \version 2.0
 */


#include <vector>
#include <string>
#include "AbstractTask.h"
#include "AbstractGridSlaveTask.h"
#include "WorkerGraph.h"

/*! \namespace Network
 * 
 * Group master/slave task template & task handling utilities (trees & graphs)
 */
namespace MasterWorkerFramework
{

/*! \class WorkTable
* \brief Utilitie to generate a batch of slave task executable in the current context
*/
class WorkTable
{
public:
	
	WorkTable(WorkerGraph* graph)
	{
		graph_ = graph;
		tasks_send = new std::vector<std::string*>();
	}
 
	/*!
	*  \brief Get the task available for processing in the current context.
	*
	*/
	std::vector<AbstractGridSlaveTask*>* GetBatch()
	{
		std::vector<AbstractGridSlaveTask*>* batch = new std::vector<AbstractGridSlaveTask*>();

		for (std::vector<WorkerTree*>::iterator worker_tree = graph_->trees_.begin(); worker_tree != graph_->trees_.end(); worker_tree++)
		{
			for (std::vector<Worker*>::iterator worker = (*worker_tree)->nodes.begin(); worker != (*worker_tree)->nodes.end(); worker++)
			{
					if ((*worker)->IsReady())
					{
						  if (!(std::find(tasks_send->begin(), tasks_send->end(),(*worker)->GetId()) != tasks_send->end()))
						  {
							batch->push_back((*worker)->GetTask());
							tasks_send->push_back((*worker)->GetId());
						  }
					}
			}
		}

		return batch;
	}


private:
	WorkerGraph* graph_;
	std::vector<std::string*>* tasks_send;

};
}

#endif